<?php

namespace App\Models\Common;

use App\Models\BaseModel;

class HolidayModel extends BaseModel
{
    protected $table = 'holidays';
    public $timestamps = false;

    protected $fillable = [
        'date', 'isHoliday', 'name'
    ];

    const WORK_TIME_HOUR_START = 9; //工作日开始时间
    const WORK_TIME_HOUR_END   = 18;  //工作日结束时间

    public static function getHolidays(int $startDate, int $endDate)
    {
        return self::query()->whereBetween('date', [$startDate, $endDate])->select('date', 'isHoliday', 'name')->get();
    }

    /**
     * 判断是否为工作日
     * @param string $date Ymd
     * @return bool
     */
    public static function checkWork(string $date = ''): bool
    {
        if (!$date) {
            $date = date('Ymd');
        }

        $date = date('Ymd', strtotime($date));

        $query = self::query()->where('date', $date)->first();

        if ($query) {
            return !$query->isHoliday;
        }

        // 没有特殊设置的日期，按照周六日为休息日
        return date('w', strtotime($date)) != 0 && date('w', strtotime($date)) != 6;
    }


    /**
     * 判断是否为工作日的工作时间
     * @param string $date Ymd
     * @return array
     */
    public static function checkWorkTime(string $date = ''): array
    {
        $isWorkDay = false;
        $isWorkDayTime= false;
        if (!$date) {
            $date = date('Y-m-d H:i:s');
        }
        $isWorkDayRes = self::checkWork($date);
        if ($isWorkDayRes) {
            $isWorkDay = true;
            $h = date('H', strtotime($date));
            if ($h >= self::WORK_TIME_HOUR_START && $h < self::WORK_TIME_HOUR_END) {
                $isWorkDayTime = true;
            }
        }
        return [$isWorkDay,$isWorkDayTime];
    }



}
